package com.liuheng.spzx.common.log.aspect;

import com.liuheng.spzx.common.log.annotation.Log;
import com.liuheng.spzx.common.log.service.AsyncOperLogService;
import com.liuheng.spzx.common.log.utils.LogUtil;
import com.liuheng.spzx.model.entity.system.SysOperLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Slf4j
@Component
public class LogAspect {
    @Autowired
    private AsyncOperLogService asyncOperLogService;
       @Around("@annotation(sysLog)")
       public Object around(ProceedingJoinPoint joinPoint, Log sysLog){
//           String title = sysLog.title();
//           log.info("LogAspect...doAroundAdvice方法执行了"+title);
//           System.out.println("LogAspect...doAroundAdvice方法执行了"+title);
           SysOperLog sysOperLog = new SysOperLog();
           LogUtil.beforeHandleLog(sysLog,joinPoint,sysOperLog);
           Object proceed = null;
           try {
               proceed=joinPoint.proceed();
               LogUtil.afterHandlLog(sysLog,proceed,sysOperLog,0,null);
           } catch (Throwable e) {
               LogUtil.afterHandlLog(sysLog,proceed,sysOperLog,1,e.getMessage());
               throw new RuntimeException(e);
           }
           asyncOperLogService.saveSysLog(sysOperLog);
           return proceed;
       }
}
